/**
 * 
 */
package com.example.radioclub.login.helper;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.example.radioclub.data.Constants;
import com.example.radioclub.music.helper.ClientRequestFactory;

import android.os.AsyncTask;
import android.util.Log;

/**
 * A Task to authenticate (or authorize?) provided User ID with
 * Radio Server. 
 * 
 * @author wizard
 *
 */
public class UserRadioAuthTask extends AsyncTask<Void, Void, String>{
	private final String TAG = Constants.APP_TAG + this.getClass().getName();
	private RestTemplate restTemplate = null;
	private IUserRadioAuthTask listener = null;
	private MultiValueMap<String, String> map = null;
	
	/**
	 * You know what this class is for, don't you?
	 * 
	 * @param userID user ID provided from another social media authentication.
	 * @param listener interface, something like that.
	 */
	public UserRadioAuthTask(int loginMethod, String userID, IUserRadioAuthTask listener) {
		// TODO Auto-generated constructor stub
		this.restTemplate = new RestTemplate(true, new ClientRequestFactory("Content-Type", " application/x-www-form-urlencoded"));
		this.listener = listener;
		this.map = new LinkedMultiValueMap<String, String>();
		
		if(loginMethod == Constants.FACEBOOK_LOGIN) {
			Log.i(TAG, "Facebook ID");
			this.map.add("Facebook", userID);
		} else if(loginMethod == Constants.GOOGLE_LOGIN) {
			Log.i(TAG, "Google ID");
			this.map.add("Google", userID);
		} else if(loginMethod == Constants.YAHOO_LOGIN) {
			Log.i(TAG, "Yahoo ID");
			this.map.add("Yahoo", userID);
		} else if(loginMethod == Constants.MSN_LOGIN) {
			Log.i(TAG, "MSN ID");
			this.map.add("MSN", userID);
		} else this.map.add("Twitter", userID);
		
/*		Log.i(TAG, (this.map.getFirst("Facebook") != null ? this.map.getFirst("Facebook") : "Facebook ID null."));
		Log.i(TAG, (this.map.getFirst("Twitter") != null ? this.map.getFirst("TWitter") : "Twitter ID null."));
		Log.i(TAG, (this.map.getFirst("Google") != null ? this.map.getFirst("Google") : "Google ID null."));
		Log.i(TAG, (this.map.getFirst("Yahoo") != null ? this.map.getFirst("Yahoo") : "Yahoo ID null."));
		Log.i(TAG, (this.map.getFirst("MSN") != null ? this.map.getFirst("MSN") : "MSN ID null."));
*/		
	}
	
	@Override
	protected String doInBackground(Void... params) {
		// TODO Auto-generated method stub
		ResponseEntity<String> response = this.restTemplate.postForEntity(Constants.API_RADIO_USER_LOGIN, this.map, String.class, this.map);
		String stat = "", cookie = null;
		try {
			stat = new JSONObject(response.getBody()).getString("stat");
			Log.i(TAG, response.getBody());
			Log.i(TAG, "Stat = " + stat);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(stat.equals("OK")) {
			cookie = response.getHeaders().get("Set-Cookie") + "";
		}
		return cookie;
	}
	
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		// if(result != null) Log.i(TAG, "Cookie for Radio Session = " + result);
		this.listener.onUserAuthenticationFinished(result);
		super.onPostExecute(result);
	}
}
